package mrpheu.demo.server.bean;

import mrpheu.demo.server.constants.MessageItemType;


public class MessageItem {
    private int sourceClient;
    private int distinctClient;
    private String message;
    private MessageItemType messageType;
    
    public MessageItem(int sourceClient, int distinctClient, String message) {
        super();
        this.sourceClient = sourceClient;
        this.distinctClient = distinctClient;
        this.message = message;
        this.messageType = MessageItemType.PEER;
    }
    
    public MessageItem(int sourceClient, String message) {
        this.sourceClient = sourceClient;
        this.distinctClient = this.sourceClient;
        this.message = message;
        this.messageType = MessageItemType.BROADCAST;
    }
    
    public int getSourceClient() {
        return sourceClient;
    }

    public void setSourceClient(int sourceClient) {
        this.sourceClient = sourceClient;
    }
    
    public int getDistinctClient() {
        return distinctClient;
    }
    
    public void setDistinctClient(int distinctClient) {
        this.distinctClient = distinctClient;
    }
    
    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    
    public MessageItemType getMessageType() {
        return messageType;
    }

    
    public void setMessageType(MessageItemType messageType) {
        this.messageType = messageType;
    }
    
    public static MessageItem processData(String line) {
        String[] message = line.split(":");
        System.out.println(message.toString());
        MessageItem messageItem = null;
        if (message.length < 2) { // sourceId-messageStr
            System.out.println("Broadcast anonymous...");
            messageItem = new MessageItem(0, message[0]);
        } else if (message.length < 3) { // messageStr
            System.out.println("Broadcast...");
            int sourceId = Integer.parseInt(message[0]);
            messageItem = new MessageItem(sourceId, message[1]);
        } else { // sourceId-distinct-messageStr
            int sourceId = Integer.parseInt(message[0]);
            int distinctId = Integer.parseInt(message[1]);
            messageItem = new MessageItem(sourceId, distinctId, message[2]);
        }
        return messageItem;
    }
    
}
